#!/usr/bin/env bash
version=
prefix=/usr/local
profile=stable
trunkRoot=svn+ssh://$USER@mono-cvs.ximian.com/source/trunk/monodevelop
branchesRoot=svn+ssh://$USER@mono-cvs.ximian.com/source/branches/monodevelop
tagsRoot=svn+ssh://$USER@mono-cvs.ximian.com/source/tags/monodevelop
destDirRoot=../..

sourceRoot=$branchesRoot

MSG=`mktemp`

usage ()
{
	echo ""
	echo "Usage : mdget [--profile=PROFILE] [--dir=DIRECTORY] [--tag] versionNumber"
	echo ""
	echo "Check outs a MonoDevelop branch or tag and the extras add-ins included"
	echo "in the specified profile (which is 'stable' by default)."
	echo ""
	echo "PROFILE is the profile that contains the list of packages to check out."
	echo ""
	echo "DIRECTORY is the root directory where MonoDevelop will be checked out."
	echo "Example: mdget --dir=/home/user/test 2.1"
	echo "This command will checkout the 2.1 branch to the directory:"
	echo "/home/user/test/monodevelop-2.1"
	echo "The output directory is ../.. by default."
	echo ""
	echo "When the --tag option is specified, a tag will be checked out,"
	echo "instead of a branch."
	echo ""
}

validate_profile ()
{
	test -z "$1" && return 0
	for c in `ls ../profiles`; do
		if [ "$c" = "$1" ]; then
			return 1
		fi
	done
	return 0
}

read_packages ()
{
	for p in `sed -e /#/d -e 's/ /,/g' < ../profiles/$profile` ; do
		path=`echo $p | cut -d ',' -f 1`
		packages="$packages $path"
	done
	return 0
}

while test x$1 != x; do
	case $1 in
		--profile=*)
			prof=`echo $1 | sed 's/--profile=//'`
			validate_profile "$prof"
			if [ $? -eq 1 ]; then
				profile=$prof
			else
				echo "Invalid profile name - $conf"
				usage
				exit 1
			fi
			;;
		--profile)
			shift
			prof=$1
			validate_profile "$prof"
			if [ $? -eq 1 ]; then
				profile=$prof
			else
				echo "Invalid profile name - $conf"
				usage
				exit 1
			fi
			;;
		--tag)
			shift
			sourceRoot=$tagsRoot
			;;
		--help)
			usage
			exit
			;;
		*)
			version=$1
			;;
	esac
	shift
done

if [ "$version" = "" ]; then
	usage
	exit
fi


read_packages

destDir=$destDirRoot/monodevelop-$version

echo ""
echo "MonoDevelop $version will be checked out from:"
echo "$sourceRoot/*/$version"
echo ""
echo "The following directories will be created:"

echo ""
for p in $packages; do
	echo "$destDir/$p"
done

while [[ 1 ]]
do 

	read -a RESPONSE  -p"Would you like to continue (Y)es/(N)o:  "
	if [[ $? != 0 ]]
	then
		RESPONSE="N"
	fi

	case $RESPONSE in
		"Y" | "y" | "yes" | "Yes")
		
			echo "Getting MonoDevelop $version ..."
			mkdir $destDir
			for p in $packages; do
				echo "svn co $sourceRoot/$p/$version $destDir/$p"
				svn co $sourceRoot/$p/$version $destDir/$p
			done
			cp ../configure $destDir/configure
			cp ../MonoDevelop.mdw $destDir/MonoDevelop.mdw
			cp ../Makefile $destDir/Makefile
			cp ../extras/extras.mdw $destDir/extras/extras.mdw
			mkdir $destDir/profiles
			cp ../profiles/$profile $destDir/profiles/all
			cp ../profiles/$profile $destDir/profiles/stable
			cp ../profiles/$profile $destDir/profiles/default
			exit 0
		;;
		
		"N" | "n" | "no" | "No")
			echo "Aborting..."
			exit 1
		;;
		*)
			# try again
		;;
	esac
done

